#!/bin/bash
#Wed Jan 19 11:35:22 CST 2022
DOWNLOAD_URL=https://gitee.com/langlyyy/node_exporter/raw/master/node_exporter-1.3.1.linux-amd64.tar.gz
DOWNLOAD_NAME=`echo $DOWNLOAD_URL|awk -F "/" '{print $NF}'`
check_exist=`ps -axuf |grep -v grep|grep node_exporter|grep -v "color"|wc -l`
if [ $check_exist -gt 0 ];then
    echo "node_exporter already exist, please check!"
    exit 1
fi

DOWNLOAD_FILE_STATUS=`curl -s -w "%{http_code}" -o /dev/null $DOWNLOAD_URL`
if [ $DOWNLOAD_FILE_STATUS -lt 400 ];then
    echo "获取安装包..."
    wget $DOWNLOAD_URL -O /tmp/$DOWNLOAD_NAME
else
    #echo $update_file " is no exist,please check file upload!"
    echo -e "\033[31m<<"$DOWNLOAD_NAME">>文件获取失败!\033[0m"
    exit 1
fi

tar -xzcf /tmp/$DOWNLOAD_NAME -C /usr/local/
cd /usr/local/ && mv node_exporter-1.3.1.linux-amd64 node_exporter

#添加服务到systemctl
cat >/etc/systemd/system/node_exporter.service <<EOF
[Unit]
Description=node exporter
Documentation=https://prometheus.io/
After=network.target

[Service]
ExecStart=/usr/local/node_exporter/node_exporter

[Install]
WantedBy=multi-user.target

EOF

systemctl daemon-reload 
systemctl start node_exporter
systemctl enable node_exporter
ss -tunlp|grep node_exporter
if [  $? != 0 ]; then
  echo "node_exporter start faild,please check!"
else
  echo "install success!"
fi

rm -f /tmp/node_exporter-1.3.1.linux-amd64.tar.gz
